<?php
//if (!defined("LEVEL") || LEVEL < 30) { die(klaida("Techninė profilaktika","<h3>Dėl techninių kliučių forumas laikinai atjungtas</h3> Tikimės greitu laiku viską sutvarkyti")); }
//Isvalom kintamuosius
unset($text,$extra);
// Gauname temos ID kur galima vilktis i ja
if (isset($url['t']) && isnum($url['t']) && $url['t'] > 0) { $tid = (int)$url['t']; } else { $tid = 0; }
// Gauname straipsnio ID ir einam i ji
if (isset($url['s']) && isnum($url['s']) && $url['s'] > 0) { $sid = (int)$url['s']; } else { $sid = 0; }
// SIaip ant prikolo, bet palikti, kad zinociau ar nori vartotojas kurti tema ar ne
if (isset($url['c']) && isnum($url['c']) && $url['c'] > 0) { $cid = (int)$url['c']; } else { $cid = 0; }
// Trinamos zinutes ID
if (isset($url['d']) && isnum($url['d']) && $url['d'] > 0) { $did = (int)$url['d']; } else { $did = 0; }
// Uzrakinamos temos ID
if (isset($url['l']) && isnum($url['l']) && $url['l'] > 0) { $lid = (int)$url['l']; } else { $lid = 0; }
//Zinutes ID
if (isset($url['e']) && isnum($url['e']) && $url['e'] > 0) { $eid = (int)$url['e']; } else { $eid = 0; }

//puslapiavimui
if (isset($tid) && isset($sid)) { 
	$viso = mysql_fetch_assoc(mysql_query1("SELECT count(*) AS viso FROM `".LENTELES_PRIESAGA."d_zinute` WHERE `tid`=".escape($tid)." AND `sid`=".escape($sid).""));
	$viso = $viso['viso'];
	$limit = 10;
}
else { $viso = 0; }

//kuris puslapis
if (!empty($url['p']) && isnum($url['p']) && $url['p'] > 0) { $p = (int)$url['p']; }
else { $p = 0; }

# asdasdasd
//  Siunciam zinute
if (isset($_POST['action']) && $_POST['action'] == 'f_send') {
	if (!isset($_SESSION['username'])) { header("Location: ?home"); }
	$zinute = $_POST['msg'];
	$tid = (int)$tid;
	$sid = (int)$sid;
	if ($tid == 0) { header("Location: ?home"); exit; }
	if ($sid == 0) { header("Location: ?home"); exit; }
	$tikrinam = mysql_fetch_assoc(mysql_query1("SELECT `uzrakinta` FROM `".LENTELES_PRIESAGA."d_straipsniai` WHERE `id`=" . escape($tid) ." AND `tid`=" . escape($sid) .""));
	if ($tikrinam['uzrakinta'] == "taip") { header("Location: ?id,25;t," . $tid .";s," . $sid .""); }
	else {
		//$uid2 =  mysql_fetch_assoc(mysql_query1("SELECT `id`, `forum_atsakyta` FROM users WHERE nick=" . escape($_SESSION['username']) .""));
		//$f_atsakyta = $uid2['forum_atsakyta'] + 1;
		if (isset($_POST['post_uid']) && $_POST['post_uid'] > 0) { $uid = (int)$_POST['post_uid']; }
		else { $uid = $_SESSION['id']; }
		mysql_query1("INSERT INTO `".LENTELES_PRIESAGA."d_zinute` (`tid`, `sid`, `nick`, `zinute`, `laikas`) VALUES (" . escape($tid) .", " . escape($sid) .", " . escape($uid) .", " . escape($zinute) .", NOW())") or die(mysql_error());
		mysql_query1("UPDATE `".LENTELES_PRIESAGA."d_temos` SET `last_data`= NOW(), `last_nick`=" . escape($_SESSION['username']) ." WHERE `id`=" . escape($tid) ."") or die(mysql_error());
		mysql_query1("UPDATE `".LENTELES_PRIESAGA."d_straipsniai` SET `last_data`= NOW(), `last_nick`=" . escape($_SESSION['username']) ." WHERE `id`=" . escape($sid) ."") or die(mysql_error());
		mysql_query1("UPDATE `".LENTELES_PRIESAGA."users` SET `forum_atsakyta`=`forum_atsakyta`+1 WHERE nick=" . escape($_SESSION['username']) ."") or die(mysql_error());
		unset($zinute,$uid,$f_atsakyta);
		//header('location: ?'.$_SERVER['QUERY_STRING'].'');
	}
}

// Sukuriam nauja straipsni/foruma
if (isset($_POST['action']) && $_POST['action'] == 'post_send') {
	if (!isset($_SESSION['username'])) { header("Location: ?home"); exit; }
	if (isset($_POST['post_uid']) && $_POST['post_uid'] > 0) { $uid = (int)$_POST['post_uid']; }
	else { $uid = $_SESSION['id']; }
	$pavadinimas = input($_POST['post_pav']);
	$zinute = $_POST['post_msg'];
	$tid = (int)$tid;
	$sid = (int)$sid;
	if (empty($pavadinimas)) { $error .= "Nenurodytas temos pavadinimas<br/>"; }
	if (empty($zinute)) { $error .= "Nenurodyta žinutė"; }
	$result = mysql_num_rows(mysql_query1("SELECT `id` FROM `".LENTELES_PRIESAGA."d_temos` WHERE `id`=" . escape($tid) .""));
	if ($result == 0) { $error .= "Blogai nurodytas forumas URL adrese. Prašome nesukčiauti.<br/>"; }
	unset($result);
	if (!$error) {
		$result = mysql_query1("INSERT INTO `".LENTELES_PRIESAGA."d_straipsniai` (`tid`, `pav`, `autorius`,`last_data`,`last_nick`) VALUES(" . escape($tid) .", " . escape($pavadinimas) . ", " . escape($_SESSION['username']) .", NOW(), " . escape($_SESSION['username']) .")");
		if (!$result) { $error .= "Klaida duomenų bazėje.<br/> Jūsų tema nebuvo sukurta.<br/> Klaida:<b> " . mysql_error() ."</b><br/>Praneškite apie klaidą administratoriui."; }
		if (!isset($error)) {
			unset ($result);
			$inf = mysql_fetch_assoc(mysql_query1("SELECT `id` FROM `".LENTELES_PRIESAGA."d_straipsniai` WHERE `pav`=" . escape($pavadinimas) ." AND autorius=" . escape($_SESSION['username']) .""));
			$result = mysql_query1("INSERT INTO `".LENTELES_PRIESAGA."d_zinute` (`tid`, `sid`, `nick`, `zinute`, `laikas`) VALUES (" . escape($tid) .", " . escape($inf['id']) .", " . escape($uid) .", " . escape($zinute) .", '" . date("Y-m-d H:i:s") ."')");
						//prirasiau kad atsinaujintu informacija apie paskutini foruma
						mysql_query1("UPDATE `".LENTELES_PRIESAGA."d_temos` SET `last_data`= NOW(), `last_nick`=" . escape($_SESSION['username']) ." WHERE `id`=" . escape($tid) ."") or die(mysql_error());
						mysql_query1("UPDATE `".LENTELES_PRIESAGA."users` SET `forum_temos`=`forum_temos`+1 WHERE nick=" . escape($_SESSION['username']) ."") or die(mysql_error());

			if (!$result) { $error .= "Klaida duomenų bazėje.<br/> Jūsų žinutė nebuvo patalpinta į jūsų sukurtą temą.<br/> Klaida:<b> " . mysql_error() ."</b><br/>Praneškite apie klaidą administratoriui."; }
			if (!isset($error)) {
				header("Location: ?id,25;t," . $tid .";s," . $inf['id'] .""); exit;
			}
		}
	}
	else {
		klaida("Klaida",$error);
	}
	unset($uid,$pavadinimas,$zinute,$error,$result,$inf);
}

// Istrinam zinute
if (isset($tid) && isset($sid) && $did > 0 && defined("LEVEL") && LEVEL >= 20) {
	$msql = mysql_query1("DELETE FROM `".LENTELES_PRIESAGA."d_zinute` WHERE `id`=".escape($did)."");
	if (mysql_affected_rows() > 0) {
		msg("Žinutė ištrinta","Pasirinkta žinutė sėkmingai ištrinta"); //redirect("?id,25;t,7,$tid;s,$sid","meta");
	}
	else {
		klaida("Klaida","Žinutė neištrinta. Prašome pasirinkti žinutę kurią norite ištrinti"); redirect("?id,25;t,$tid;s,$sid","meta");
	}
}

// Uzrakinam/Atrakinam tema
if ((int)$lid != 0) {
	if (defined("LEVEL") && LEVEL >= 20) {
        unset($sql);
		$sql = mysql_fetch_assoc(mysql_query1("SELECT `uzrakinta` FROM `".LENTELES_PRIESAGA."d_straipsniai` WHERE `tid`=" . escape($tid) ." AND `id`=" . escape($lid) .""));
		$lock = $sql['uzrakinta'];
		unset($sql);
		if ($lock == "ne") {
			$result = mysql_query1("UPDATE `".LENTELES_PRIESAGA."d_straipsniai` SET `uzrakinta`='taip' WHERE `d_straipsniai`.`id`=".escape($lid)."")  or die(mysql_error());
			if ($result) { header("Location: ?id,25;t," . $tid .""); exit; }
		}
		if ($lock == "taip") {
			$result = mysql_query1("UPDATE `".LENTELES_PRIESAGA."d_straipsniai` SET `uzrakinta`='ne' WHERE `d_straipsniai`.`id`=".escape($lid)."") or die(mysql_error());;
			if ($result) { header("Location: ?id,25;t," . $tid .""); exit; }
		}
		unset($result,$d_info,$sql);
	}
	unset($sql);
}

// Trinti tema
if ((int)$tid && (int)$did && (int)$sid == 0) {
	if (defined("LEVEL") && LEVEL >= 20) {
		$result = mysql_query1("DELETE FROM `".LENTELES_PRIESAGA."d_straipsniai` WHERE `d_straipsniai`.`id`=".escape($did)."");
		if ($result) {
			$result = mysql_query1("DELETE FROM `".LENTELES_PRIESAGA."d_zinute` WHERE `d_zinute`.`sid`=".escape($did)."");
			if ($result) { header("Location: ?id,25;t," . $tid .""); exit; }
		}
	}
}

//Perziureti pasirinkta straipsni
if (isset($tid) && (int)$tid && (int)$sid && (int)$cid == 0) {
	// Kokiam mes straipsny?
	$tsql = mysql_fetch_assoc(mysql_query1("SELECT `pav` FROM `".LENTELES_PRIESAGA."d_straipsniai` WHERE `tid`=" . escape((int)$tid) ." AND `id`=" . escape((int)$sid) .""));
	$straipsnis = $tsql['pav'];
	unset($tsql);

	// Is kokios temos atsivilkom
	$tsql = mysql_fetch_assoc(mysql_query1("SELECT `pav` FROM `".LENTELES_PRIESAGA."d_temos` WHERE `id`=" . escape($tid) .""));
	$tema = $tsql['pav'];
	unset($tsql);
	$f_text = "<a href='?id,25'>Forumas</a> > <a href='?id,25;t," . $tid ."'>". $tema ." </a> ><b> " . $straipsnis ."</b>";
	lentele("Diskusijų forumas",$f_text);
    unset($f_text,$tema);
	// Gaunam reikalinga INFO apie vartotoja kur parase zinute i TAM TIKRA staripsnio ID
	$sql = "SELECT `".LENTELES_PRIESAGA."users.nick`, `".LENTELES_PRIESAGA."users.avatar`, `".LENTELES_PRIESAGA."users.taskai`, `".LENTELES_PRIESAGA."users.levelis`, `".LENTELES_PRIESAGA."users.gim_data`, `".LENTELES_PRIESAGA."users.id`, `".LENTELES_PRIESAGA."users.miestas`, `".LENTELES_PRIESAGA."users.icq`, `".LENTELES_PRIESAGA."users.msn`, `".LENTELES_PRIESAGA."users.skype`, `".LENTELES_PRIESAGA."users.aim`, `".LENTELES_PRIESAGA."users.url`, `".LENTELES_PRIESAGA."users.yahoo`, `".LENTELES_PRIESAGA."users.forum_atsakyta`, `".LENTELES_PRIESAGA."d_zinute.id` AS `zid`, `tid`, `sid`,`zinute`,`laikas`  FROM `".LENTELES_PRIESAGA."users` INNER JOIN `".LENTELES_PRIESAGA."d_zinute` ON `".LENTELES_PRIESAGA."d_zinute.nick`=`".LENTELES_PRIESAGA."users.id` WHERE `sid`=" . escape((int)$sid) ." ORDER BY laikas ASC LIMIT $p,$limit";
	$sql = mysql_query1($sql);
	$row2 = mysql_fetch_assoc($sql);
	if ($row2['levelis'] >= 20 || defined("LEVEL") && LEVEL >= 20) {
		$sql2 = mysql_fetch_assoc(mysql_query1("SELECT `uzrakinta` FROM `".LENTELES_PRIESAGA."d_straipsniai` WHERE `id`=$sid"));
		if ($sql2['uzrakinta'] == "taip") {
			echo "<a href='?id,25;t," . $tid .";l," . $sid ."'><img src='images/forum/atrakinti.gif' border=0 class='middle' alt='accept'/></a>";
		}
		if ($sql2['uzrakinta'] == "ne") {
			echo "<a href='?id,25;t," . $tid .";l," . $sid ."'><img src='images/forum/uzrakinti.gif' border=0 class='middle' alt='uzrakinti'/></a>";
		}
		echo "<a href='?id,25;t," . $tid .";d," . $sid ."'><img src='images/forum/trinti.gif' border=0 class='middle' alt='trinti'/></a><br/><br/>";
		unset($sql2);
	}
	unset($row2,$sql);
 	//$sql = "SELECT users.nick, users.avatar, users.taskai, users.levelis, users.gim_data, users.id, users.miestas, users.icq, users.msn, users.skype, users.aim, users.url, users.yahoo, users.forum_atsakyta, d_zinute.id AS `zid`, `tid`, `sid`,`zinute`,`laikas`  FROM users INNER JOIN `d_zinute` ON d_zinute.nick=users.id WHERE `sid`='" . $sid ."' ORDER BY laikas ASC LIMIT $p,$limit";
	$sql = "SELECT ".LENTELES_PRIESAGA."users.nick, ".LENTELES_PRIESAGA."users.avatar, ".LENTELES_PRIESAGA."users.taskai, ".LENTELES_PRIESAGA."users.levelis, ".LENTELES_PRIESAGA."users.gim_data, ".LENTELES_PRIESAGA."users.id, ".LENTELES_PRIESAGA."users.miestas, ".LENTELES_PRIESAGA."users.icq, ".LENTELES_PRIESAGA."users.msn, ".LENTELES_PRIESAGA."users.skype, ".LENTELES_PRIESAGA."users.aim, ".LENTELES_PRIESAGA."users.url, ".LENTELES_PRIESAGA."users.yahoo, ".LENTELES_PRIESAGA."users.forum_atsakyta, ".LENTELES_PRIESAGA."d_zinute.id AS `zid`, ".LENTELES_PRIESAGA."d_zinute.nick AS `nikas`, `tid`, `sid`,`zinute`,`laikas` FROM ".LENTELES_PRIESAGA."users INNER JOIN `".LENTELES_PRIESAGA."d_zinute` ON ".LENTELES_PRIESAGA."d_zinute.nick=".LENTELES_PRIESAGA."users.id WHERE `sid`='" . $sid ."' ORDER BY laikas ASC LIMIT $p,$limit";
	$sql = mysql_query1($sql);
	$a = 0; $text = '';
	while ($row = mysql_fetch_assoc($sql)) {
		$a++;
		$text .= "
			<fieldset>
			<table border='0' width='100%'>
				<tr>
					<td width='10%' valign='top'>
					<center><a href='?id,47;m," . $row['id'] ."'>" . $row['nick'] ."</a></center>";
						if ($row['levelis'] == 1) { $text .= "<img src='images/levelis/1.gif' alt='1'/><br />"; }
    					if ($row['levelis'] == 2) { $text .= "<img src='images/levelis/2.gif' alt='2'/><br />"; }
    					if ($row['levelis'] == 3) { $text .= "<img src='images/levelis/3.gif' alt='3'/><br />"; }
    					if ($row['levelis'] == 4) { $text .= "<img src='images/levelis/4.gif' alt='4'/><br />"; }
    					if ($row['levelis'] == 5) { $text .= "<img src='images/levelis/5.gif' alt='5'/><br />"; }
    					if ($row['levelis'] == 6) { $text .= "<img src='images/levelis/6.gif' alt='6'/><br />"; }
    					if ($row['levelis'] == 7) { $text .= "<img src='images/levelis/7.gif' alt='7'/><br />"; }
    					if ($row['levelis'] == 8) { $text .= "<img src='images/levelis/8.gif' alt='8'/><br />"; }
    					if ($row['levelis'] == 9) { $text .= "<img src='images/levelis/9.gif' alt='9'/><br />"; }
    					if ($row['levelis'] == 10) { $text .= "<img src='images/levelis/10.gif' alt='10'/><br />"; }
    					if ($row['levelis'] == 20) { $text .= "<img src='images/levelis/mod.gif' alt='mod'/><br />"; }
    					if ($row['levelis'] == 30) { $text .= "<img src='images/levelis/admin.gif' alt='admin'/><br />"; }
    					$text .= "
    					<br/>
						<img src='" . $row['avatar'] ."' alt='kauke'/><br/>
						<br/>
						Amžius: " . amzius($row['gim_data']) ."<br/>
						Taškai: " . $row['taskai'] ."<br/>
						Miestas: " . $row['miestas'] . "<br/>
						Žinutės: " . $row['forum_atsakyta'] . "<br/>
					</td>
					<td valign='top'>
					<table width='100%' border='0'>
						<tr>
							<td valign='top' style=\"width:100%; hight:100%\" class=\"title\">
								Laikas: " . (($row['laikas']=='0000-00-00 00:00:00')?'---':$row['laikas']) ."
							</td>
						</tr>
						<tr>
							<td valign='top' width='100%' height='150' style=\"background-color:white; padding:10px; border-bottom:1px dashed silver;\" >
							<span class=\"forum\">".bbcode($row['zinute'])."</span>
							</td>
						</tr>
					</table>
					</td>
				</tr>
				<tr>
					<td></td>
					<td valign='bottom'>
					<table width='100%' border='0'>
						<tr>
							<td width=60%>
								<fieldset>";
								$text .= "<a href='?id,45;n,1;u," . $row['nick'] ."'><img src='images/forum/icon_pm.gif' border='0' alt='PM' /></a>&nbsp;&nbsp;";
								if(!empty($row['icq'])) { $text .= "<a href='http://wwp.icq.com/scripts/search.dll?to=" . urlencode($row['icq']) ."' target='_blank'><img src='images/forum/icon_icq.gif' border='0' alt='icq' /></a>&nbsp;&nbsp;"; }
								if(!empty($row['msn'])) { $text .= "<a href='http://members.msn.com/" . urlencode($row['msn']) ."' target='_blank'><img src='images/forum/icon_msnm.gif' border=0 alt='msn' /></a>&nbsp;&nbsp;"; }
								if(!empty($row['aim'])) { $text .= "<a href='aim:goim?screenname=" . urlencode($row['aim']) ."&message=Ar+Esi?' target='_blank'><img src='images/forum/icon_aim.gif' border=0 alt='aim'/></a>&nbsp;&nbsp;"; }
								if(!empty($row['yahoo'])) { $text .= "<a href='http://edit.yahoo.com/config/send_webmesg?.target=" . urlencode($row['yahoo']) ."&.src=pg' target='_blank'><img src='images/forum/icon_yahoo.gif' border=0 alt='yahoo'/></a>&nbsp;&nbsp;"; }
								if(!empty($row['skype'])) { $text .= "<a href='skype:" . urlencode($row['skype']) ."?chat' target='_blank'><img src='http://mystatus.skype.com/mediumicon/".urlencode($row['skype'])."' width='16px' hight='16' border=0 alt='skype' /></a>&nbsp;&nbsp;"; }
								if(!empty($row['url'])) { $text .= "<a href='".$row['url']."' target='_blank'><img src='images/forum/icon_www.gif' border=0 alt='www' /></a>&nbsp;&nbsp;"; }
								$text .= "</fieldset>
							</td>";
							if (isset($_SESSION['id']) && $row['nikas'] == $_SESSION['id'] || defined("LEVEL") && LEVEL >= 20) {
								$text .= "\n<td align='right'><fieldset>";
								$text .="<a href='".url("e," . $row['zid'] ."")."#end'><img src='images/forum/icon_edit.gif' border='0' alt='edit'/></a>";
								if ($a != 1) { 
									$text .= "<a href='?id,25;t," . $tid .";s," . $sid .";d," . $row['zid'] ."'><img src='images/forum/icon_trinti.gif' border='0' alt='trinti'/></a>";
								}
								$text .= "\n</fieldset>\n</td>";
							}
							$text .="	
						</tr>
					</table>
					</td>
				</tr>
			</table>
			</fieldset>
		";
	}
	lentele($straipsnis,puslapiai($p,$limit,$viso,10).$text.puslapiai($p,$limit,$viso,10)."<a name='end' id='end'></a>");
	unset($text,$straipsnis,$sql,$row);
}

// Jei norima kurti nauja straipsni
if (isset($tid) && (int)$tid && (int)$cid != 0) {
	if (!isset($_SESSION['username'])) { header("Location: ?home"); }
	$uid = $_SESSION['id'];
	unset($temp_info);
	$text = "
		<fieldset>
		<legend>Naujos temos sukūrimas</legend>
		<form name=\"post_msg\" action=\"\" method=\"post\">
		<table border='0' widht='100%'>
			<tr>
				<td width='10%'>Autorius:</td>
				<td><b>" . $_SESSION['username'] ."</b><input type=\"hidden\" name=\"post_uid\"  value='" . $uid ."' /></td>
			</tr>
			<tr>
				<td>Pavadinimas:</td>
				<td><input name=\"post_pav\" type=\"text\" size=\"50\" value=\"\"/>
			</tr>
			<tr>
				<td valign='top' align='left'>Pranešimas:</td>
				<td><textarea name=\"post_msg\" id=\"post_msg\" rows=10 style=\"width:100%\" wrap=\"on\"></textarea><br />
				".bbk("post_msg")."
				</td>
			</tr>
		</table>
		<input type=\"submit\" value=\"Siųsti\"/>
		<input type=\"hidden\" name=\"action\"  value=\"post_send\" />
		</form>
		</fieldset>
	";
	lentele("Naujos temos sukūrimas",$text); unset($text);

}

// Jei TEMOS ID gavom ...
if (isset($tid) && (int)$tid && (int)$sid == 0 && (int)$cid == 0) {
	include_once("priedai/class.php");
	$bla = new Table();
	$info = array();
	// Sugaudom visus straipsnius is pasirinkto temos
	$sql = mysql_query1("SELECT * FROM `".LENTELES_PRIESAGA."d_straipsniai` WHERE `tid`=" . escape($tid) ." ORDER BY `id` DESC") or die(mysql_error());

	// Kokios mes temoj?
	$tsql = mysql_fetch_assoc(mysql_query1("SELECT pav FROM ".LENTELES_PRIESAGA."d_temos WHERE `id`=" . escape($tid) .""));
	$tema = $tsql['pav'];
	unset($tsql);
	$f_text = "<a href='?id,25'>Forumas</a> ><b> " . $tema ."</b> >";
	lentele("Diskusijų forumas",$f_text);
 	if (isset($_SESSION['username'])) {
			echo "<a href='?id,25;t," . $tid .";c,1'><img src='images/forum/post.gif' border=0 alt='rasyti'/></a><br/><br/>";
	}
	// Parodom straipsniu sarasa is musu pasirinkto temos
	while($row = mysql_fetch_assoc($sql)) {
		$tsql = mysql_fetch_assoc(mysql_query1("SELECT count(id) AS viso FROM `".LENTELES_PRIESAGA."d_zinute` WHERE `tid`=" . escape($tid) ." AND `sid`=" . escape($row['id']) . ""));
		//$tsql = kiek("d_zinute","WHERE `tid`=" . escape($tid) ." AND `sid`=" . escape($row['id']) . "");
		$zinutes = $tsql['viso'];
		unset($tsql);
		if ($row['uzrakinta'] == 'taip') { $extra = "<img src='images/forum/locked.png' alt='užrakintas' />"; } else { $extra = "<img src='images/icons/transmit.png' alt='tema' />"; }
	
		$info[]=array(
			""=> $extra,
			"Tema:"=>"<a href='?id,25;t," . $tid .";s," . $row['id'] ."' style='display:block'>" . $row['pav'] . ' '. naujas($row['last_data'])."</a>",
			"Žinučių:"=>$zinutes,
			"Kada:"=> $row['last_nick'].' - '.(($row['last_data']=='0000-00-00 00:00:00')?'':kada($row['last_data']))
		);
	}
	


	//asort($info);
	//lentele("Gautos žinutės",puslapiai($p,$limit,$pm_sk,10)."<br/>".$bla->render($info)."<br/>".puslapiai($p,$limit,$pm_sk,10));

	//lentele("Temų sąrašas",$text);
	lentele("Temų sąrašas",$bla->render($info));
	unset($text,$sql,$tema,$row,$zinutes,$f_text);
}

// Nu Jei temos negavom ... Parodom visas temas
if (isset($tid) && (int)$tid == 0) {
	//Ruošiamės atvaizduoti forumo temas
	include_once("priedai/class.php");

	// Pagrindine nuoroda jei niekas nepaspausta
	$f_text = "<b>Forumas</b> > ";
	lentele("Diskusijų forumas",$f_text);

	// Kokius mes tem forumo pavadinimus turim?
	$d_forumai = mysql_query1("SELECT * FROM ".LENTELES_PRIESAGA."d_forumai ORDER BY id");

	// Parodomos visos temos esanciame forume
	while ($row = mysql_fetch_assoc($d_forumai)) {
		$db = mysql_num_rows(mysql_query1("SELECT count(`id`) AS total FROM ".LENTELES_PRIESAGA."d_temos WHERE `fid`=" . escape($row['id']) .""));
		// Radom temu forume. Parodom
		if ($db != 0) {
			$sql2 = mysql_query1("SELECT * FROM ".LENTELES_PRIESAGA."d_temos WHERE `fid`=" . escape($row['id']) ."");
			$f_info = "<a href=\"?id,25#".$row['id']."\" name=\"".$row['id']."\" id=\"".$row['id']."\"><img src=\"images/icons/anchor.png\" alt=\"#\" class=\"middle\" border=\"0\" /></a> ".$row['pav'];

			$bla = new Table();
			$info = array();

			//Apdirbam gautus duomenis ir isvedam daba tikrai temas
			while ($row2 = mysql_fetch_assoc($sql2)) {
				$tsql = mysql_fetch_assoc(mysql_query1("SELECT count(`id`) AS viso FROM `".LENTELES_PRIESAGA."d_straipsniai` WHERE `tid`=" . escape($row2['id']) .""));
				$temos = $tsql['viso'];
				unset($tsql);
				$tsql = mysql_fetch_assoc(mysql_query1("SELECT count(`id`) AS viso FROM `".LENTELES_PRIESAGA."d_zinute` WHERE `tid`=" . escape($row2['id']) .""));
				$zinutes = $tsql['viso'];
				unset($tsql);
				$info[]=array(
					//""=> $extra,
					"Forumas:"=>"<a href='?id,25;t," . $row2['id'] ."'><span style='font-size:12px'>" . $row2['pav'] ."</span></a> ".naujas($row2['last_data'])."<br/><i><span style='font-size:9px'>" . $row2['aprasymas'] ."</span></i>",
					"Temų:"=>$temos,
					"Žinučių:"=> $zinutes,
					"Kada:" => $row2['last_nick'].' - '.(($row2['last_data']=='0000-00-00 00:00:00')?'':kada($row2['last_data']))
				);
			}
			lentele($f_info,$bla->render($info)."<div style='padding-bottom:20px'></div>");
			unset($sql2,$row2,$text,$f_info,$temos,$zinutes,$info);
		}
	}
}

//Jei adminas/moderatorius spaudzia redaguoti
if (defined("LEVEL") && LEVEL > 20 && isnum($eid) && $eid > 0) {
	$extra = '';
	//Array ( [msg] => [php][/php]p [action] => f_update )
	if (!empty($_POST['msg']) && $_POST['action'] == 'f_update') {
		mysql_query1("UPDATE `".LENTELES_PRIESAGA."d_zinute` SET `zinute`=".escape($_POST['msg']."\n[sm][i]Redagavo:".$_SESSION['username']."[/i][/sm]")." WHERE `id`=".escape($eid)."") or die(mysql_error());
		redirect("?id,25;t,$tid;s,$sid;p,$p");
	}
	else {
		$sql = "SELECT * FROM `".LENTELES_PRIESAGA."d_zinute` WHERE `id`=" . escape($eid) ." LIMIT 1";
		$sql = mysql_query1($sql);
		$sql = mysql_fetch_assoc($sql);
		$extra = $sql['zinute'];
	}
}
// Ar vartotojas uzsiregistraves? Jei taip leidziam parasyti zinute
if (isset($_SESSION['username']) && (int)$sid > 0) {
	$sql = mysql_fetch_assoc(mysql_query1("SELECT `uzrakinta` FROM `".LENTELES_PRIESAGA."d_straipsniai` WHERE `tid`=" . escape($tid) ." AND id=" . escape($sid) .""));
	if ($sql['uzrakinta'] == "ne") {
		$text = "
			<fieldset>
			<legend>".((!empty($extra))?"Redaguoti":"Siųsti naują")." žinutę</legend>
			<center>
				<form name=\"msg\" action=\"\" method=\"post\">
					<textarea name=\"msg\" id=\"msg\" rows=10 style=\"width:100%\" wrap=\"physical\">".((!empty($extra))?input($extra):'')."</textarea><br/>
					".bbk('msg')."
					<br />
					<input type=\"submit\" value=\"".((!empty($extra))?"Redaguoti":"Siųsti")."\"/>
					<input type=\"hidden\" name=\"action\"  value=\"".((!empty($extra))?"f_update":"f_send")."\" />
				</form>
			</center>
			</fieldset>
		";
		hide("Naujos žinutės siuntimas",$text,(!empty($extra))?false:true);
		unset($text);
	}
	unset($sql);
}

?>
